package com.ait.edu.controller;


import com.ait.commonutils.Msg;
import com.ait.edu.entity.Course;
import com.ait.edu.entity.vo.CourseInfoVo;
import com.ait.edu.entity.vo.CoursePublishVo;
import com.ait.edu.service.CourseService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-06-14
 */
@RestController
@RequestMapping("/eduservice/course")
public class CourseController {


    @Autowired
    private CourseService courseService;


    @ApiOperation("添加课程")
    @PostMapping("addCourse")
    public Msg addCourse(@RequestBody CourseInfoVo courseInfoVo){
       String id = courseService.saveCourseInfo(courseInfoVo);
        return Msg.success().data("id",id);
    }



    @ApiOperation("ID获取课程")
    @GetMapping("getCourseId")
    public Msg getCourseId(String id){
        CourseInfoVo courseInfoVo = courseService.getCourseInfoVoId(id);
        if(courseInfoVo != null){
            return Msg.success().data("course",courseInfoVo);
        }
        return Msg.fail();
    }

    @ApiOperation("获取全部课程")
    @GetMapping("getCourselist")
    public Msg getCourselist(){
        List<Course> list = courseService.list(null);
        if(list != null){
            return Msg.success().data("list",list);
        }
        return Msg.fail();
    }


    @ApiOperation("修改课程")
    @PutMapping("modifyCourseId")
    public Msg modifyCourseId(@RequestBody CourseInfoVo courseInfoVo){
        String s = courseService.modifyCourseInfoVo(courseInfoVo);

        return Msg.success().data("id",s);
    }


    @ApiOperation("获取课程基本信息")//就是一个链表查询
    @GetMapping("getCoursePublishV")
    public Msg getCoursePublishById(String id){
        CoursePublishVo coursePublishV = courseService.getCoursePublishV(id);
        if(coursePublishV != null){
            return Msg.success().data("coursePublish",coursePublishV);
        }
        return Msg.fail();
    }

    @ApiOperation("课程上传")
    @PutMapping("releaseCourse")
    public Msg releaseCourse( String id){
        Course course = new Course();
        course.setId(id);
        course.setStatus("Normal");
        if (courseService.updateById(course)) {
            return Msg.success();
        }

        return Msg.fail();
    }

    @ApiOperation("删除课程")
    @DeleteMapping("removeCourse")
    public Msg removeCourse(String id){
        if (courseService.removeCourse(id)){
            return Msg.success();
        }
        return Msg.fail();
    }



}

